// Copyright (c) 2002 Cunningham & Cunningham, Inc.
// Read license.txt in this directory.

package eg.music
{

import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.events.Event;
import com.fit4as.fit.Exception;

[Event(name="loadComplete")]
public class MusicLibrary extends EventDispatcher {

    public static const EVENT_LOAD_COMPLETE : String = "loadComplete";

    private static var _instance : MusicLibrary;

    public static function get instance() : MusicLibrary
    {
        if (_instance == null) _instance = new MusicLibrary();
        return _instance;
    }


    public var looking : Music = null;
    public var library : Array = [];

    public function load(url : String) : void
    {
        //t race("MusicLibrary#load " + url );
        var loader : URLLoader = new URLLoader();
        loader.dataFormat = URLLoaderDataFormat.TEXT;
        var request : URLRequest = new URLRequest(url);
        loader.addEventListener("complete", loadComplete);
        loader.load(request);
    }

    protected function loadComplete(evt : Event) : void
    {
        //t race("MusicLibrary#loadComplete 1" + [ library, evt, evt.target	 ] );
        var input : String = String(URLLoader(evt.target).data);
        var lines : Array = input.split("\n");
        for (var i : int = 1; i < lines.length; i++)
        {
            library.push(Music.parse(lines[ i ]));
        }
		//t race("MusicLibrary#loadComplete 2" + library );
        dispatchEvent(new Event(EVENT_LOAD_COMPLETE));
    }

    public function select(m : Music) : void {
        //t race("MusicLibrary#select " + m );
        looking = m;
    }

    public function search(seconds : Number) : void {
        Music.status = "searching";
        Simulator.nextSearchComplete = Simulator.schedule(seconds);
    }

    public function searchComplete() : void {
        Music.status = MusicPlayer.playing == null ? "ready" : "playing";
    }

    public function findAll() : void {
        search(3.2);
        for (var i : int = 0; i < library.length; i++) {
            library[i].selected = true;
        }
    }

    public function findArtist(a : String) : void {
        search(2.3);
        for (var i : int = 0; i < library.length; i++) {
            library[i].selected = library[i].artist == a;
        }
    }

    public function findAlbum(a : String) : void {
        search(1.1);
        for (var i : int = 0; i < library.length; i++) {
            library[i].selected = library[i].album == a;
        }
    }

    public function findGenre(a : String) : void {
        search(0.2);
        for (var i : int = 0; i < library.length; i++) {
            library[i].selected = library[i].genre == a;
        }
    }

    public function findYear(a : int) : void {
        search(0.8);
        for (var i : int = 0; i < library.length; i++) {
            library[i].selected = library[i].year == a;
        }
    }

    public function displayCount() : int {
        var count : int = 0;
        for (var i : int = 0; i < library.length; i++) {
            count += (library[i].selected ? 1 : 0);
        }
        return count;
    }

    public function displayContents() : Array {
        var displayed : Array = new Array();
        for (var i : int = 0, j : int = 0; i < library.length; i++) {
            if (library[i].selected) {
                displayed[j++] = library[i];
            }
        }
        return displayed;
    }

}
}